package com.oa.hyperion.api.v1;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.oa.hyperion.entity.Post;
import com.oa.hyperion.exception.http.NotFoundException;
import com.oa.hyperion.service.PostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 岗位controller
 */

//使用RestController =controller+ ResponseBody
@Api(value="岗位controller",tags={"岗位操作接口"})
@RestController
@RequestMapping("/v1/post")
public class PostController {

    @Autowired
    private PostService postService;

    /**
     * 获取所有岗位
     */
    @ApiOperation("获取所有岗位")
    @GetMapping("getAll")
    public List<Post> getAll(){
        return postService.list();
    }

    @ApiOperation("获取指定部门的可用岗位")
    @GetMapping("getPostWithDepartmentId/{id}")
    public List<Post> getPostWithDepartmentId(@PathVariable Integer id){
            QueryWrapper<Post> wrapper=new QueryWrapper<>();
            wrapper.eq("department",id);
            wrapper.eq("status","y");
        return postService.list(wrapper);
    }

    @ApiOperation("获取所有岗位带部门的信息")
    @GetMapping("getPostList")
    public List<Post> getPostList(){
        return postService.getPostWithDepartment();
    }


    /**
     * 添加岗位
     */
    @ApiOperation("添加岗位")
    @PostMapping("add")
    public String addPost(@RequestBody Post post){
        post.setStatus("y");

        if(postService.save(post)) {
            return "添加成功";
        }
        return "添加失败";
    }


    /**
     * 逻辑删除,岗位
     */
    @ApiOperation("逻辑删除,岗位")
    @DeleteMapping("delete")
    public String deletePost(Integer id){
        Post post=new Post();
        post.setId(id);
        post.setStatus("n");

        if(postService.updateById(post)) {
            return "删除成功";
        }
        return "删除失败";
    }

    /**
     * 修改岗位
     */
    @ApiOperation("修改岗位")
    @PutMapping("update")
    public String updatePost(@RequestBody Post post){

        //获取要修改的对象
        Post p=postService.getById(post.getId());
        //有效性校验
        if(p==null){
            throw new NotFoundException(404);
        }

        //修改岗位信息
        if(p.getName()!=null){
            p.setName(post.getName());
        }

        if(p.getDepartment()!=null){
            p.setDepartment(post.getDepartment());
        }
        if(p.getStatus()!=null){
            p.setStatus(post.getStatus());
        }


        if( postService.updateById(p)){
            return "修改成功";
        }
        return "修改失败";
    }

}
